*****************************************************************************
* Cleaning and Analyzing - PRITM countries. Alternative proxy				*
*   . Polarization proxy as Dalton Index                                    *
*						Manifesto Project Database                          *
*  																            *
* Author: 			Valentina Gonzalez-Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			March 21 2021 									        *
* Version:			Stata 17						                        *
*																			*
*****************************************************************************

/*
This do-file:
	A. Call the Data
	B. Define variables
	C. Export Tables 

Input: ** Manifesto Project database**
	- Data\CMP\MPDataset_MPDS2020a_stata14.dta // Data download from https://manifesto-project.wzb.eu/datasets 2021


Final output:
	Cleaned data: 
		* "Data\CMP_Dalton.dta" this data contains the relevant variables for the analysis with the DV as the polarization over redistribution, and fixed-value positions, estimated as Dalton index
	Tables:
		* table A22: Partisan Polarization over Redistribution and Fixed Attributes, Dalton Index


*/
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication" // Only change your directory

* Processing of the data (alternatively skip and go to line 175)
{
*##########################################
* A. Calling the data
*##########################################
{

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", replace

}

*############################################
* B. Creating Variables
*############################################
{
* YEAR
gen year = year(edate) 

* Keep countries of interest
keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

* Defines a dummy to identify PRITM countries
gen PRITM=. 
replace PRITM=0 if countryname=="Australia"
replace PRITM=0 if countryname=="Canada"
replace PRITM=0 if countryname=="Greece"
replace PRITM=0 if countryname=="New Zealand"
replace PRITM=0 if countryname=="Portugal"
replace PRITM=0 if countryname=="Spain"
replace PRITM=0 if countryname=="United Kingdom"
replace PRITM=0 if countryname=="United States"
replace PRITM=0 if countryname=="France"

replace PRITM=1 if countryname=="Austria"
replace PRITM=1 if countryname=="Belgium"
replace PRITM=1 if countryname=="Denmark"
replace PRITM=1 if countryname=="Estonia"
replace PRITM=1 if countryname=="Finland"
replace PRITM=1 if countryname=="Germany"
replace PRITM=1 if countryname=="Hungary"
replace PRITM=1 if countryname=="Iceland"
replace PRITM=1 if countryname=="Ireland"
replace PRITM=1 if countryname=="Italy"
replace PRITM=1 if countryname=="Netherlands"
replace PRITM=1 if countryname=="Norway"
replace PRITM=1 if countryname=="Slovenia"
replace PRITM=1 if countryname=="Slovakia"
replace PRITM=1 if countryname=="Sweden"
replace PRITM=1 if countryname=="Switzerland"


* CONTROL VARIABLES - number of parties

gen number = 1
egen number2= sum(number), by(edate)
lab var number2 "Number of parties"

*############################################################
* DEPENDENT VARIABLE Redistribution and Fixed Attributes
*############################################################
{
* Obtaining relevant policy variables

gen welfare_policy =  ln(per504+0.5) - ln(per505+0.5)

gen fixed = .
replace fixed = (ln(per107+per108+per407+per602+0.5+per602_2)-ln(per109+per110+per406+per601+0.5+per601_2)) if !missing(per602_2) & !missing(per601_2)
replace fixed = (ln(per107+per108+per407+per602+0.5)-ln(per109+per110+per406+per601+0.5)) if missing(per602_2) | missing(per601_2)

}

** Different code compared to 3_2 do file: 
***** Now Dalton Index
{
egen pervote_e= sum(pervote), by(edate partyname)

foreach x of varlist welfare_policy fixed  {

egen `x'_av= mean(`x'), by(edate countryname)
}	

foreach x of varlist welfare_policy fixed  {

gen `x'_d= pervote_e*(`x'-`x'_av)^2
}

foreach x of varlist welfare_policy fixed  {

egen `x'_dt= sum(`x'_d), by(edate countryname)
gen `x'_dt2= (`x'_dt)^0.5
}	
}

* Collapsing the data 
{
collapse (first) year  PRITM  totseats number2 oecdmember date *_dt2 *_dt , by(edate countryname)
}
// Final Prep of the data
{
egen country_number = group(countryname)

sort country_number edate
bysort country_number: gen election_order=_n
xtset country_number election_order


sort countryname  election_order




sort countryname  year

gen shock=.
replace shock =0 if year > 1969 & year < 1995
replace shock =1 if  year > 1994
xtset country_number election_order
}
}
*############################################
* Saving the data
*############################################
{
lab var PRITM "PR with Trichotomous Multipartism"
lab var totseats "Total Number of Seats"
lab var number2 "Total Number of Parties"
lab var oecdmember "OECD member"
lab var welfare_policy_dt2 "Distance Redistribution (DR) - Net Welfare"
lab var fixed_dt2 "Distance Fixed-Value Positions (DFVP) - Net Anti-Global"
lab var PRITM "PRITM"
lab var shock "High LMP period" // post 1994

keep  PRITM year  countryname  oecdmember totseats shock welfare_policy_dt2 fixed_dt2   number2 election_order country_number

keep if year>1969
keep if PRITM==1

save "Data\CMP_Dalton.dta", replace

}
}
* Alternatively load prepared data
{
use "Data\CMP_Dalton.dta", clear	
}
*##########################################
* Analysis
*##########################################
{
// table A22: Partisan Polarization over Redistribution and Fixed Attributes, Dalton Index
{
preserve
keep if PRITM==1
   keep if year>1969 

eststo clear
// Redistribution
eststo: qui reg welfare_policy_dt2 L.welfare_policy_dt2 shock totseats  oecdmember  number2 i.year, cluster(countryname)
eststo: qui reg fixed_dt2 L.fixed_dt2 shock totseats  oecdmember  number2 i.year, cluster(countryname)



esttab , replace label se title(Polarization over Redistribution and Fixed Attributes between Mainstream Left and Right-Populist \label {TableCMPAllFE}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tots*" "LDV = L.*" "FE Year = *year") 

esttab using "Table\TabWithindalton.tex", replace label se title(Polarization over Redistribution and Fixed Attributes, Dalton Index \label {TabWithindalton}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tots*" "LDV = L.*" "FE Year = *year") 



restore
}
}